<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/admin/category/list">类别管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>添加类别</el-breadcrumb-item>
        </el-breadcrumb>

        <h3 style="margin: 20px 0;">添加类别</h3>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
            <el-form-item label="父级类别id" prop="parentId">
                <el-input v-model="ruleForm.parentId"></el-input>
            </el-form-item>
            <el-form-item label="类别名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="图标图片的URL" prop="icon">
                <el-input v-model="ruleForm.icon"></el-input>
            </el-form-item>
            <el-form-item label="关键词列表" prop="keywords">
                <el-input v-model="ruleForm.keywords"></el-input>
            </el-form-item>
            <el-form-item label="是否启用" prop="enable">
                <el-switch
                        v-model="ruleForm.enable"
                        active-value="1"
                        inactive-value="0"
                        active-color="#13ce66"
                        inactive-color="#999">
                </el-switch>
            </el-form-item>
            <el-form-item label="是否显示在导航栏" prop="isDisplay">
                <el-switch
                        v-model="ruleForm.isDisplay"
                        active-value="1"
                        inactive-value="0"
                        active-color="#13ce66"
                        inactive-color="#999">
                </el-switch>
            </el-form-item>
            <el-form-item label="自定义排序序号" prop="sort">
                <el-input v-model="ruleForm.sort"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                ruleForm: {
                    name: '',
                    parentId: '',
                    keywords: '',
                    sort: '',
                    icon: '',
                    enable: true,
                    isDisplay: true
                },
                rules: {
                    name: [
                        {required: true, message: '请输入类别名称', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    sort: [
                        {pattern: /^[1-9]{1}[0-9]?$/, message: '必须是 0 - 99 之间的数值', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url = 'http://localhost:9080/categories/add-new';
                        console.log('url >>> ' + url);
                        console.log('data >>> ');
                        console.log(this.ruleForm);
                        this.axios
                            .create({headers: {'Authorization': localStorage.getItem('jwt')}})
                            .post(url, this.ruleForm).then((response) => {
                            console.log(response.data);
                            if (response.data.code == 20000) {
                                this.$message({
                                    message: '添加类别成功！',
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(response.data.message);
                            }
                        }).catch(function(error) {
                            console.log('响应结果为失败！');
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>

<style scoped>

</style>